# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
import requests

headers = {
    "Host": "wpc.mobis.co.kr",
    "ClientSign": "770cdc18aaaa57306122e3711ccf2e6b",
    "Key": "bf33add5-fb5a-44ec-985d-52d2825d0bc8",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
    "Referer": "http://wpc.mobis.co.kr/Index/Index.jsp",
    "Cookie": "JSESSIONID=0000KR_gUfu-jWdZF_IlEN7mZYm:1b9i8aoq5; WPCLOGIN=Nv_Fqd1ZPxCAhgssuXMR9p_",
    'Accept':'*/*',
    'Accept-Language':'zh-CN,zh;q=0.8;q=0.8',
}
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36',
    # 'Cookie':'JSESSIONID=0000PswAoVbkEB9DcJZutYeBhvi:1b9i8aoq5; WPCLOGIN=PswAoVbkEB9DcJZutYeBhvi',
    'Host':'wpc.mobis.co.kr',
    'Key':'bf33add5-fb5a-44ec-985d-52d2825d0bc8',
    'ClientSign':'770cdc18aaaa57306122e3711ccf2e6b',
    'Referer':'http://wpc.mobis.co.kr/Index/Index.jsp',
    'Accept':'*/*',
    'Accept-Language':'zh-CN,zh;q=0.8;q=0.8',
}
cookies = {
    "JSESSIONID": "0000KR_gUfu-jWdZF_IlEN7mZYm:1b9i8aoq5",
    "WPCLOGIN": "Nv_Fqd1ZPxCAhgssuXMR9p_",
}
post_url = '''http://wpc.mobis.co.kr/IllustSearch?cmd=getUniqueGroupKeyList'''

# Host:wpc.mobis.co.kr
# ClientSign:770cdc18aaaa57306122e3711ccf2e6b
# Key:bf33add5-fb5a-44ec-985d-52d2825d0bc8
# User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
# X-Requested-With:XMLHttpRequest
# Referer:http://wpc.mobis.co.kr/Index/Index.jsp
# Cookie:JSESSIONID=0000KR_gUfu-jWdZF_IlEN7mZYm:1b9i8aoq5; WPCLOGIN=Nv_Fqd1ZPxCAhgssuXMR9p_
# POST http://wpc.mobis.co.kr/IllustSearch?cmd=getUniqueGroupKeyList HTTP/1.1

post_data = {'VIN': '',
             'CRNM': 'KX3 ELECTRIC 19',
             'BPNO': 'CCHNPCQ19',
             'HKGB': 'K',
             'HSGB': 'E',
             'PKGB': 'P',
             'OPTY': 'E',
             'PCCD': 'P',
             'VIEW': 'Y',
             'EFIN': '20181001',
             'EFOT': '',
             'VHCD': 'O20',
             'GRTY': 'CQ',
             'CRYY': '',
             'REG1': '',
             'SGMT': 'A',
             'UCCS': '',
             'UCDT': '',
             'UCWT': '',
             'UC01': '',
             'UC02': '',
             'UC03': '',
             'UC04': '',
             'UC05': '',
             'UC06': '',
             'UC07': '',
             'UC08': '',
             'UC09': '',
             'UC10': '',
             'UCCT': 'EP',
             'FOPT': '',
             'ISVIN': 'MODEL',
             'YEAR': '',
             'NAIF': '',
             'MICD': '',
             'OCN': '',
             'VER': '',
             'VGTAG': '',
             'ECRD': '',
             'ICRD': '',
             'REG': '',
             'EFAC': '',
             'MFAC': '',
             'ENCD': '',
             'TMCD': '',
             'STCD': '',
             'PDATE': '',
             'SDATE': '',
             'ENNO': '',
             'TMNO': '',
             'TXKEY': '',
             'AS': 'N',
             'FSC': '',
             'MATCH': 'ON',
             'LRCD': 'MO',
             'LANG': 'CH',
             'UGNO': '',
             'GRNO': '',
             'PNCD': '',
             'PTNO': '',
             'PTNM': '',
             'TEXT': '',
             'FLAG': '',
             'PCNO': '',
             }
# proxies = {'http': '101.132.46.34:5870'}
proxies = {'http': '106.14.190.253:5870'}
# respose = requests.post(url=post_url, data=post_data, headers=headers, cookies=cookies, proxies=proxies)
# print(respose.content.decode())


# GET http://wpc.mobis.co.kr/illust/GROUP/CCHNPCQ19/PNG/09-09111.png?time=1544611885465 HTTP/1.1
# imgurl = 'http://wpc.mobis.co.kr/illust/GROUP/CCHNPCQ19/{}/{}.{}?time={}'
# imgurl = 'http://wpc.mobis.co.kr/illust/GROUP/CCHNPCQ19/TIF/20-21611.gif?time={}'
import time
def get_img_url(cid, img_name, format):
    t = time.time().__str__().replace('.', '')[:13]
    _dict = {
        'png':{'folder':'PNG'},
        'gif':{'folder':'TIF'}
    }
    base_url = 'http://wpc.mobis.co.kr/illust/GROUP/{}/{}/{}.{}?time={}'
    return base_url.format(cid, _dict[format]['folder'], img_name, format, t)
img_name = '4343912'
base_url = 'http://wpc.mobis.co.kr/illust/GROUP/CCHNPCQ19/PNG/43-43912.png?time={}'.format(time.time().__str__().replace('.', '')[:13])


def get_imgs(self, cid, img_list):
    def get_img_url(cid, img_name, format):
        t = time.time().__str__().replace('.', '')[:13]
        _dict = {
            'png': {'folder': 'PNG'},
            'gif': {'folder': 'TIF'}
        }
        base_url = 'http://wpc.mobis.co.kr/illust/GROUP/{}/{}/{}.{}?time={}'
        return base_url.format(cid, _dict[format]['folder'], img_name, format, t)

    format_l = ['gif', 'png']
    for img_name in img_list:
        for format in format_l:
            img_url = get_img_url(cid, img_name, format)
            response = requests.get(url=img_url, headers=headers, cookies=cookies, allow_redirects=False)
            status_code = response.status_code
            if status_code == 302:
                continue
            else:
                with open('./{}.{}'.format(img_name, format), 'wb') as i:
                    i.write(response.content)
                break



# url = 'http://wpc.mobis.co.kr/illust/GROUP/CCHNPSBC10/TIF/20-203A11.gif?time=1545113159273'
#
# res = requests.get(url=base_url, headers=headers, cookies=cookies, proxies=proxies, allow_redirects=False)
# with open('./4343912.png', 'wb') as g:
#     g.write(res.content)
# _dict = {'a':1, 'b':2}
# import os
# b_path = '/Users/duanmengdi/Desktop/workspace/py3_code/hk/kia_img/'
# l = os.listdir(b_path)
# for name in l:
#     if name == 'CCHNPCQ19':
#         continue
#     else:
#         new_name = name.replace('CCHNPCQ19.', '')
#         comd = 'mv "{}" "{}"'.format(b_path+name, b_path+new_name)
#         os.system(comd)
# import hashlib
# def get_md5(str):
#     '''获取md5加密电子指纹'''
#     md5 = hashlib.md5()
#     md5.update(str)
#     return md5.hexdigest()
#
# _dict1 = {"a":"a", "b":"b"}
# _dict2 = {"b":"b", "a":"a"}
# if _dict1 == _dict2:
#     print('xxx')

# s = '''1\:20-201\:1730\:681\:135\:44\:SUB ENGINE\^1\:20-202\:1730\:719\:135\:44\:短距离发动机总成\^1\:20-203\:1730\:623\:135\:44\:发动机成套衬垫\^1\:20-211\:1730\:758\:132\:44\:气缸体\^1\:20-213\:787\:546\:135\:44\:前壳体\^1\:20-215\:881\:854\:135\:44\:皮带罩和油底壳\^1\:20-216\:840\:146\:135\:44\:发动机和变速驱动桥支架\^1\:20-221\:1482\:291\:135\:44\:气缸盖\^1\:20-224\:1053\:189\:135\:44\:气门摇摆室盖\^1\:20-231\:1730\:835\:135\:44\:曲轴和活塞\^1\:20-240\:1316\:291\:135\:44\:凸轮轴和阀门\^1\:25-251\:858\:719\:135\:44\:冷却液泵\^1\:25-253\:273\:440\:135\:44\:发动机冷却装置\^1\:25-255\:1730\:565\:135\:44\:冷却液软管和管道\^1\:29-291\:1507\:1028\:135\:44\:下盖\^2\:09-091\:1187\:1112\:135\:44\:维修工具\^2\:28-281\:1072\:804\:135\:44\:空气滤清器\^2\:28-282\:42\:783\:135\:44\:涡轮增压器和中冷器\^2\:28-283\:201\:489\:135\:44\:进气岐管\^2\:28-285\:695\:249\:135\:44\:排气岐管\^2\:28-286\:1062\:160\:135\:44\:MUFFLER & EXHAUST PIPE\^2\:28-289\:136\:311\:135\:44\:VAPORIZER CONTROL SYSTEM\^2\:28-390\:1594\:485\:135\:44\:电子控制\^2\:31-310\:863\:223\:135\:44\:燃油系统\^2\:31-313\:1027\:749\:135\:44\:燃油管\^2\:31-351\:778\:302\:135\:44\:节气门体和喷嘴\^2\:32-327\:1720\:457\:135\:44\:加速踏板\^2\:32-328\:1653\:992\:135\:44\:制动器和离合器踏板\^2\:37-371\:379\:1034\:135\:44\:蓄电池和电缆线\^2\:39-273\:247\:393\:135\:44\:火花塞和电缆\^2\:39-361\:673\:1052\:135\:44\:起动机\^2\:39-373\:67\:580\:135\:44\:交流发电机\^2\:39-394\:399\:198\:135\:44\:电磁阀\^\@09-091\:维修工具\:09-091\:\:\:\:1\^20-201\:SUB ENGINE\:20-201B\:\:\:1600 CC - GAMMA\:1\^20-201\:SUB ENGINE\:20-201A\:\:\:1400 CC - KAPPA\:2\^20-202\:短距离发动机总成\:20-202B\:\:\:1600 CC - GAMMA\:1\^20-202\:短距离发动机总成\:20-202A\:\:\:1400 CC - KAPPA\:2\^20-203\:发动机成套衬垫\:20-203A\:\:\:1400 CC - KAPPA\:1\^20-203\:发动机成套衬垫\:20-203B\:\:\:1600 CC - GAMMA\:2\^20-211\:气缸体\:20-211A\:\:\:1400 CC - KAPPA\:1\^20-211\:气缸体\:20-211B\:\:\:1600 CC - GAMMA\:2\^20-213\:前壳体\:20-213A\:\:\:1400 CC - KAPPA\:1\^20-213\:前壳体\:20-213B\:\:\:1600 CC - GAMMA\:2\^20-215\:皮带罩和油底壳\:20-215A\:\:\:1400 CC - KAPPA\:1\^20-215\:皮带罩和油底壳\:20-215B\:\:\:1600 CC - GAMMA\:2\^20-216\:发动机和变速驱动桥支架\:20-216A\:\:\:1400 CC - KAPPA\:1\^20-216\:发动机和变速驱动桥支架\:20-216B\:\:\:1600 CC - GAMMA\:2\^20-221\:气缸盖\:20-221A\:\:\:1400 CC - KAPPA\:1\^20-221\:气缸盖\:20-221B\:\:\:1600 CC - GAMMA\:2\^20-224\:气门摇摆室盖\:20-224A\:\:\:1400 CC - KAPPA\:1\^20-224\:气门摇摆室盖\:20-224B\:\:\:1600 CC - GAMMA\:2\^20-231\:曲轴和活塞\:20-231A\:\:\:1400 CC - KAPPA\:1\^20-231\:曲轴和活塞\:20-231B\:\:\:1600 CC - GAMMA\:2\^20-240\:凸轮轴和阀门\:20-240A\:\:\:1400 CC - KAPPA\:1\^20-240\:凸轮轴和阀门\:20-240B\:\:\:1600 CC - GAMMA\:2\^25-251\:冷却液泵\:25-251A\:\:\:1400 CC - KAPPA\:1\^25-251\:冷却液泵\:25-251B\:\:\:1600 CC - GAMMA\:2\^25-253\:发动机冷却装置\:25-253\:\:\:\:1\^25-255\:冷却液软管和管道\:25-255A\:\:\:1400 CC - KAPPA\:1\^25-255\:冷却液软管和管道\:25-255B\:\:\:1600 CC - GAMMA\:2\^28-281\:空气滤清器\:28-281A\:\:\:1400 CC - KAPPA\:1\^28-281\:空气滤清器\:28-281B\:\:\:1600 CC - GAMMA\:2\^28-282\:涡轮增压器和中冷器\:28-282A\:\:\:1400 CC - KAPPA\:1\^28-282\:涡轮增压器和中冷器\:28-282B\:\:\:1600 CC - GAMMA\:2\^28-283\:进气岐管\:28-283A\:\:\:1400 CC - KAPPA\:1\^28-283\:进气岐管\:28-283B\:\:\:1600 CC - GAMMA\:2\^28-285\:排气岐管\:28-285A\:\:\:1400 CC - KAPPA\:1\^28-285\:排气岐管\:28-285B\:\:\:1600 CC - GAMMA\:2\^28-286\:MUFFLER & EXHAUST PIPE\:28-286A\:\:\:1400 CC - KAPPA\:1\^28-286\:MUFFLER & EXHAUST PIPE\:28-286B\:\:\:1600 CC - GAMMA\:2\^28-289\:VAPORIZER CONTROL SYSTEM\:28-289A\:\:\:1400 CC - KAPPA\:1\^28-390\:电子控制\:28-390A\:\:\:1400 CC - KAPPA\:1\^28-390\:电子控制\:28-390B\:\:\:1600 CC - GAMMA\:2\^29-291\:下盖\:29-291\:\:\:\:1\^31-310\:燃油系统\:31-310\:\:\:\:1\^31-313\:燃油管\:31-313A\:\:\:1400 CC - KAPPA\:1\^31-313\:燃油管\:31-313B\:\:\:1600 CC - GAMMA\:2\^31-351\:节气门体和喷嘴\:31-351A\:\:\:1400 CC - KAPPA\:1\^31-351\:节气门体和喷嘴\:31-351B\:\:\:1600 CC - GAMMA\:2\^32-327\:加速踏板\:32-327\:\:\:\:1\^32-328\:制动器和离合器踏板\:32-328\:\:\:\:1\^37-371\:蓄电池和电缆线\:37-371\:\:\:\:1\^39-273\:火花塞和电缆\:39-273A\:\:\:1400 CC - KAPPA\:1\^39-273\:火花塞和电缆\:39-273B\:\:\:1600 CC - GAMMA\:2\^39-361\:起动机\:39-361A\:\:\:1400 CC - KAPPA\:1\^39-361\:起动机\:39-361B\:\:\:1600 CC - GAMMA\:2\^39-373\:交流发电机\:39-373A\:\:\:1400 CC - KAPPA\:1\^39-373\:交流发电机\:39-373B\:\:\:1600 CC - GAMMA\:2\^39-394\:电磁阀\:39-394B\:\:\:1600 CC - GAMMA\:1\^'''
#
#
# def parse_structrue_info(response):
#     # 解析主组下,分组的信息
#     res_0 = response.split("\@")[0].split('\^')
#     res_1 = response.split("\@")[1].split('\^')
#     _l0 = []
#     _l1 = []
#     for info in res_0:
#         if info == '':
#             continue
#         info_l = info.split('\:')
#         # print(info_l)
#         _dict = dict(
#             img_num=info_l[0],  # 描点在第几张图片上, 暂时没有意义
#             mid=info_l[1],
#             mid_imgPoint=[info_l[2], info_l[3], info_l[4], info_l[5]],
#             mid_name=info_l[6]
#         )
#         _l0.append(_dict)
#     for info1 in res_1:
#         if info1 == '':
#             continue
#         info1_l = info1.split('\:')
#         # print(info1_l)
#         _dict1 = dict(
#             mid=info1_l[0],  # 暂时不用此参数作为mid
#             structrue_name=info1_l[1],
#             GRNO=info1_l[2],  # 请求分组下数据用的code, 会跟mid不一样, 用此参数当做mid
#             i3=info1_l[3],
#             i4=info1_l[4],
#             remark=info1_l[5],  # 分组备注, 此参数有内容的不多
#             i6=info1_l[6],
#         )
#         _l1.append(_dict1)
#     return _l0, _l1
#
#
# l0, l1 = parse_structrue_info(s)
#
# for i in range(len(l1)):
#     print(l1[i])


s1 = '''D2[123456]'''
s2 = '''K4'''
s3 = 'K4[123,456]'
s4 = 'K4,K5[123,456]'

import re
def func(s):
    if s and '[' in s:  # 有sa
        sa = re.findall('\[(.*?)\]', s)[0]
        uc = s.replace(sa, "").strip(" []")
        uc = uc.split(",") if uc else []
        sa = sa.strip().split(",") if sa.strip() else []
    elif s and '[' not in s:  #只有uc
        sa = []
        uc = s.strip().split(',') if s.strip() else []
    else:
        sa = []
        uc = []
    return sa, uc

print(func(s4))









